package com.hcss.core.common.filemgt.dao;

import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.hcss.core.common.filemgt.model.AnnexFileModel;
import com.hcss.core.mvc.BaseDao;



/**  
 *
 * @ClassName: AnnexFileDao
 * @author: lvfang 
 * @date: 2014年2月21日 下午2:40:17
 * @version: V1.0  
 */
@Repository("annexFileDao")
public class AnnexFileDao extends BaseDao{
    private static final String TBLPREFIX = "t_annex";

    public static String table() {
        return TBLPREFIX;
    }
	
	 /**
     * @Title: insertAnnex
     * <p>新增附件信息<p>
     * @param model
     * @return
     * @return: int
     */
    public int insertAnnex(AnnexFileModel model){
    	if (model == null) {
            return 0;
        }
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT INTO ");
        sql.append(table());
        sql.append(" (fileName, fileType,fileSize,filePath,\"upDate\",creator,buinessKey,buinessId) VALUES (?, ?, ?,?, ?, ?,?, ?)");
        Object[] params = new Object[] { 
        		model.getFileName(),
        		model.getFileType(),
        		model.getFileSize(),
        		model.getFilePath(),
        		new Date(),
        		0,
        		model.getBuinessKey(),
        		model.getBuinessId()
        };
        return this.insertSelective(sql.toString(), params);
    }
	
    /**
     * @Title: findById
     * <p>查询附件信息<p>
     * @param annexId
     * @return
     * @return: AnnexFileModel
     */
    public AnnexFileModel findById(int annexId){
    	String sql = "select * from "+table()+" where id=?";
    	RowMapper<?> rowMapper = new BeanPropertyRowMapper<AnnexFileModel>(AnnexFileModel.class);
    	return (AnnexFileModel) this.jdbcTemplate.queryForObject(sql, rowMapper,annexId);
    }
    
    /**
     * @Title: getFilelistByBussId
     * <p>根据业务Id和业务类型 查询<p>
     * @param busId
     * @return
     * @return: List<AnnexFileModel>
     */
    @SuppressWarnings("unchecked")
	public List<AnnexFileModel> getFilelistByBussId(int busId,String bussType){
    	String sql = "select * from "+table()+" where buinessId=? and buinessKey = ?";
    	RowMapper<?> rowMapper = new BeanPropertyRowMapper<AnnexFileModel>(AnnexFileModel.class);
    	return (List<AnnexFileModel>) this.jdbcTemplate.query(sql, rowMapper,busId,bussType);
    }
    
    /**
     * @Title: deleteFileById
     * <p>根据Id删除附件<p>
     * @param fileId
     * @return: void
     */
    public int deleteFileById(int fileId){
    	String sql = "delete from " + table() + " where id =" + fileId;
        return jdbcTemplate.update(sql);
    }
    
    /**
     * @Title: deleteFileByBusiness
     * <p>根据业务删除附件<p>
     * @param fileId
     * @return: void
     */
    public void deleteFileByBusiness(int bussinessId,String bussinessKey){
    	String sql = "delete from " + table() + " where buinessId=? and buinessKey = ?";
        jdbcTemplate.update(sql,bussinessId,bussinessKey);
    }
    
    /**
     * 修改附件业务主键Id
     * @param paramaryId
     * @param buinessId
     * @return
     */
    public int updateBusinessIdById(int paramaryId,int buinessId){
    	String sql = "update "+table()+" set buinessId = ? where id = ?";
    	Object[] params = new Object[]{
    			buinessId,
    			paramaryId
    	};
    	return jdbcTemplate.update(sql, params);
    }
}
